<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>

    const Person = (_ => {
      function Person(name, age, gender) {
        this.name = name
        this.age = age
        this.gender = gender
      }
      let instance
      return function (name, age, gender) {
        if (!instance) instance = new Person(name, age, gender)
        return instance
      }
    })()

    // new 关键字 可有可无
    let singleton1 = new Person('henry', 18, 'male')
    let singleton2 = new Person('elsiy', 20, 'female')
    console.log(singleton1) //=> {name: "henry", age: 18, gender: "male"}
    console.log(singleton2) //=>  {name: "henry", age: 18, gender: "male"}
    console.log(singleton1 === singleton2) //=> true



  </script>
</body>

</html>